<template>

    <el-container class="home_container">
      <el-header>
        <div>
          <img src="../assets/logo.png" style="width=50px;height:50px;">
          <span>vue电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>

      <el-container>
        <el-aside :width="hiddenFlag?'64px':'200px'">
          <div class="home_toggle" @click="hiddenToggle">|||</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            unique-opened
            :collapse="hiddenFlag"
            :collapse-transition="false"
            :default-active="currentMenu"
            router

          >

            <!-- 一级目录 -->
            <el-submenu :index="menu.id+''" v-for="menu in menusList" :key="menu.id">
              <!-- 一级目录的图标区域 -->
              <template slot="title">
                <i :class="iconObj[menu.id]"></i>
                <span>{{menu.authName}}</span>
              </template>

              <!-- 二级目录 -->

              <el-menu-item :index="sub.path" v-for="sub in menu.children" :key="sub.id" @click="saveCurrentIndex(sub.path)">
                <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{sub.authName}}</span>
              </template>
              </el-menu-item>

            </el-submenu>

          </el-menu>

        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>

      </el-container>
    </el-container>

</template>

<script>
export default {
  data () {
    return {
      menusList: [],
      iconObj: {
        101: 'iconfont iconyonghuguanli',
        102: 'iconfont iconquanxianguanli',
        103: 'iconfont iconshangpinguanli',
        104: 'iconfont icondingdanguanli',
        105: 'iconfont iconshujutongji'
      },
      hiddenFlag: false,
      currentMenu: ''
    }
  },
  created () {
    this.getMenusList()
    this.currentMenu = window.sessionStorage.getItem('currentIndex')
  },
  methods: {

    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenusList () {
      const { data: res } = await this.$http.get('/menus')
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }

      this.menusList = res.data
    },
    hiddenToggle () {
      this.hiddenFlag = !this.hiddenFlag
    },
    saveCurrentIndex (currentIndex) {
      window.sessionStorage.setItem('currentIndex', currentIndex)
      this.currentMenu = currentIndex
    }
  }

}
</script>

<style lang="less" scoped>
.el-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  color: #fff;
  padding-left: 2px;
  align-items: center;  //这个也是display: flex;布局下的语句，表示 容器内的元素居中
  font-size: 20px;
  > div{
    display:flex;
    align-items: center;
    span{
      margin-left: 12px;
    }
  }
}

.el-aside{
  background-color: #333744;
}

.el-main{
background-color: #eaedf1;
}

.home_container{
  height: 100%;
}

.iconfont{
  margin-right: 10px;
}

.el-menu{
  border-right:none;
}
.home_toggle{
  background-color: #4a5064;
  color: white;
  font-size: 10px;
  line-height: 24px; //行高
  text-align: center;//HTML元素中的文本排列到道中间的意思。
  letter-spacing: 0.2em;//word-spacing 属性增加或减少单词间的空白（即字间隔）。 letter-spacing 属性增加或减少字符间的空白（字符间距）
  cursor: pointer;//光标呈现为指示链接的指针（一只手）

}
</style>
